﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLDAOL.Models;
using QLDAOL.Util;
using QLDAOL.ModelView;

namespace QLDAOL.Controllers
{
    public class TaskController : BaseController
    {
        //
        // GET: /Index/
        public IEQLDAOLRepository repository = new IEQLDAOLRepository();
        public ActionResult ListTaskPage(int ID_PJ = 0)
        {
            int userID = 1; //Danh
            int id = int.Parse(Session["pjID"].ToString());
            bool checkaccess = false;
            if (id > 0)
                checkaccess = repository.checkPJAccess(id, userID);
            if (checkaccess)
            {
                var lstTask = repository.getlstTask(id);
                ViewData["lstTask"] = lstTask;
                return View();
            }
            return RedirectToAction("AccessDenied");
        }

        public ActionResult ListTask()
        {
            int id = int.Parse(Session["pjID"].ToString());
            var lstTask = repository.getlstTask(id);
            ViewData["lstTask"] = lstTask;
            return PartialView();
        }
        public ActionResult ListTask2()
        {
            int userid = 1;//Danh
            var lstTask = repository.getlstTask(userid,2);
            ViewData["lstTask"] = lstTask;
            return PartialView("ListTask");
        }
        public ActionResult AddTask()
        {
            int id = int.Parse(Session["pjID"].ToString());
            var lstUser = repository.getlstUserRoleByPJID(id);
            var lstState = repository.getlstTaskState();
            ViewData["lstState"] = lstState;
            ViewData["lstUser"] = lstUser;
            return PartialView();
        }
        public ActionResult AddTaskAct()
        {
            string task = Request.Form["task"].ToString();
            string start = Request.Form["start"].ToString();
            string dead = Request.Form["dead"].ToString();
            int status= int.Parse(Request.Form["status"].ToString());
            int id = int.Parse(Session["pjID"].ToString());
            int userid=int.Parse(Request.Form["user"].ToString());
            repository.addTask(task,userid, start, dead,id, status);
            return RedirectToAction("ListTask");
        }

        public ActionResult EditTask()
        {
            int taskID = int.Parse(Request.Form["taskID"].ToString());
            int task_id = int.Parse(Request.Form["taskID"].ToString());
            int id = int.Parse(Session["pjID"].ToString());
            var lstUser = repository.getlstUserRoleByPJID(id);
            var lstState = repository.getlstTaskState();
            ViewData["lstState"] = lstState;
            ViewData["lstUser"] = lstUser;
            var task= repository.gettask(task_id);
            ViewData["task"] = task;
            ViewData["taskID"] = taskID;
            return PartialView();
        }
        public ActionResult EditTaskAct()
        {
            int taskID = int.Parse(Request.Form["taskID"].ToString());
            string task = Request.Form["task"].ToString();
            string start = Request.Form["start"].ToString();
            string dead = Request.Form["dead"].ToString();
            int status = int.Parse(Request.Form["status"].ToString());
            int id = int.Parse(Session["pjID"].ToString());
            int userid = int.Parse(Request.Form["user"].ToString());
            repository.editTask(taskID,task, userid, start, dead, id, status);
            return RedirectToAction("ListTask");
        }
    }
}
